import 'package:flutter_tts/flutter_tts.dart';

class TtsUtil {
  static TtsUtil? _instance;

  ///判空符??
  factory TtsUtil() => _instance ?? TtsUtil._internal();

  late FlutterTts _flutterTts;

  TtsUtil._internal() {
    _flutterTts = FlutterTts();
    _instance = this;
  }

  Future speak(String text) async {
    /// 设置语言
    //await _flutterTts.setLanguage("zh-CN");

    /// 设置音量
    await _flutterTts.setVolume(1);

    /// 设置语速
    await _flutterTts.setSpeechRate(0.5);

    /// 音调
    await _flutterTts.setPitch(1.0);

    if (text.isNotEmpty) {
      await _flutterTts.speak(text);
    }
    }

  /// 暂停
  Future _pause() async {
    await _flutterTts.pause();
  }

  /// 结束
  Future _stop() async {
    await _flutterTts.stop();
  }
}
